package cn.tpshion.devops.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -4996659426371181721L;

    private String code;

    private String msg;

    private T data;

    public static <T> Result<T> build(ErrorCode errorCode, T data){
        return new Result(errorCode.getCode(), errorCode.getMsg(), data);
    }

    public static <T> Result<T> ok(T data){
        return build(ErrorCode.OK, data);
    }

    public static <T> Result<T> ok(){
        return build(ErrorCode.OK, null);
    }

    public static Result fail(ErrorCode errorCode){
        return new Result(errorCode.getCode(), errorCode.getMsg(), null);
    }

    public static Result fail(String msg){
        return new Result(ErrorCode.FAIL.getCode(), msg, null);
    }

    public static Result error(CoreException exception){
        return new Result(exception.getCode(), exception.getMsg(), null);
    }
}
